package com.stone.persistent.recyclerview.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;


import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.stone.persistent.recyclerview.helper.CarouselHelper;



public class CarouselConstraintLayout extends ConstraintLayout {
    private CarouselHelper carouselHelper;

    public boolean dispatchTouchEvent(@Nullable MotionEvent ev) {

        CarouselHelper carouselHelper;
        if (ev.getAction() == 0) {
            carouselHelper = this.carouselHelper;
            if (carouselHelper != null) {
                carouselHelper.stop();
            }
        } else if (ev.getAction() == 1 || ev.getAction() == 3) {
            carouselHelper = this.carouselHelper;
            if (carouselHelper != null) {
                carouselHelper.start();
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        CarouselHelper carouselHelper = this.carouselHelper;
        if (carouselHelper != null) {
            carouselHelper.start();
        }
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        CarouselHelper carouselHelper = this.carouselHelper;
        if (carouselHelper != null) {
            carouselHelper.stop();
        }
    }

    public  void startCarousel(ViewPager2 viewPager) {
        this.carouselHelper = new CarouselHelper(viewPager);
        CarouselHelper mCarouselHelper = this.carouselHelper;
        if (mCarouselHelper != null) {
            mCarouselHelper.start();
        }
    }

    public CarouselConstraintLayout( Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CarouselConstraintLayout(Context var1, AttributeSet var2, int var3, int var4) {
        this(var1, var2, var3);
    }

    public CarouselConstraintLayout( Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0, 4);
    }

    public CarouselConstraintLayout( Context context) {
        this(context, null, 0, 6);
    }
}
